<?php

namespace app\api\controller;

use app\admin\model\About;
use app\admin\model\goods\Artist;
use app\admin\model\Goods\Classify;
use app\admin\model\goods\Product;
use app\admin\model\Home;
use app\admin\model\News;
use app\admin\model\Notice;
use app\common\controller\Api;
use app\common\model\Attachment;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 跑马灯
     */
    public function notice()
    {
        $model = new Notice();
        $row   = $model->find();
        $this->success('请求成功', $row);
    }

    /**
     * about
     */
    public function about()
    {
        $model = new About();
        $row   = $model->find();
        $this->success('请求成功', $row);
    }

    /**
     * 新闻
     */
    public function news()
    {
        $data = News::field('id,name,desc,createtime')->order("createtime", 'desc')->select();
        $this->success('请求成功', $data);
    }

    public function new_show()
    {
        $id   = $this->request->param('id', '');
        $data = News::find($id);
        $this->success('请求成功', $data);
    }

    /**
     * 产品分类
     */
    public function classify()
    {
        $data = Classify::order('weight', 'desc')->select();
        array_unshift($data, [
            'createtime' => 1632027590,
            'id'         => 1000000000,
            'name'       => "全部 ALL",
            'updatetime' => 1634194152,
            'weight'     => 1
        ]);
        $this->success('请求成功', $data);
    }

    /**
     * 艺术家分类
     */
    public function artist()
    {
        $data = Artist::order('weight', 'desc')->select();
        $this->success('请求成功', $data);
    }

    /**
     * 产品
     */
    public function product()
    {
        $classify_id = $this->request->param('classify_id', '');
        if ($classify_id == 1000000000) {
            $classify_id = '';
        }
        $artist_id = $this->request->param('artist_id', '');
        $is_all    = $this->request->param("all", '');

        $data = Product::where(function ($query) use ($classify_id, $artist_id, $is_all) {
            if ($is_all == '') {
                if ($classify_id != '') {
                    $query->where("find_in_set(" . $classify_id . ", classify_ids)");
                }

                if ($artist_id != '') {
                    $query->where("find_in_set(" . $artist_id . ", artist_ids)");
                }
            } else {
                if ($is_all == 1) {
                    $query->where('classify_ids', '<>', '');
                } else {
                    $query->where('artist_ids', '<>', '');
                }
            }

        })->order("weight", 'desc')->paginate(10);

        $home = new Home();
        $home = $home->find();
        $max  = $home->max_width;
        $min  = $home->min_width;

        foreach ($data as &$datum) {
            $datum->is_show     = true;
            $attachment         = Attachment::where(['url' => $datum->image])->find();
            $datum->image       = !empty($datum->image) ? cdnurl($datum->image, true) : '';
            $datum->imagewidth  = $attachment->imagewidth;
            $datum->imageheight = $attachment->imageheight;
            if ($datum->random_l) {
                $random_l          = explode(",", $datum->random_l);
                $random            = array_rand($random_l, 1);
                $randomInt         = $random_l[$random];
                $imagewidth        = ceil($datum->imagewidth * ($randomInt / 100));
                $datum->imagewidth = $imagewidth;
            }

            if ($datum->imagewidth > $max) {
                $datum->imagewidth = $max == '' ? $datum->imagewidth : 0;
            } else if ($datum->imagewidth < $min) {
                $datum->imagewidth = $min == '' ? $datum->imagewidth : 0;
            }
        }
        $this->success('请求成功', $data);
    }

    public function time()
    {
        $home = new Home();
        $home = $home->find();
        if ($home) {
            $home->times *= 1000;
            $home->music = $home->music ? cdnurl($home->music, true) : '';
        }
        $this->success('请求成功', $home);
    }
}
